package com.epoint.mobileoa.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.FrameParam;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.ui.personchoose.model.PersonChooseInfoModel;


/**
 * @author liyc
 * @time 2012-12-19 上午10:36:37
 * @annotation 
 */
public class NewDbUtil {
	public DBOpenHelp dbOpenHelper;
	public FrameParam fp;
	public LogHelp logHelp;
	public Context con;
	public NewDbUtil(Context con){
		this.con = con;
		fp = (FrameParam) con.getApplicationContext();
		dbOpenHelper = new DBOpenHelp(con, con.getString(R.string.dbname));
	}
	
	public List<PersonChooseInfoModel> getPersonChoose(String parentguid)
	{
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select ouname,ouguid from ou where parentguid = ? ",new String[] { parentguid });
		List<PersonChooseInfoModel> plist = new ArrayList<PersonChooseInfoModel>();
		while (cursor.moveToNext()) {
			PersonChooseInfoModel p = new PersonChooseInfoModel();
			p.name = cursor.getString(0);
			p.guid = cursor.getString(1);
			p.type = "部门";
			p.parentguid = parentguid;
			plist.add(p);
		}

		cursor = db.rawQuery(
				"select displayname,userguid,ou.ouname from user,ou where  user.ouguid = ou.ouguid and user.ouguid =?",
				new String[] { parentguid });

		while (cursor.moveToNext()) {
			PersonChooseInfoModel p = new PersonChooseInfoModel();
			p.name = cursor.getString(0);
			p.guid = cursor.getString(1);
			p.type = "人员:"+cursor.getString(2);
			p.parentguid = parentguid;
			plist.add(p);
		}
		cursor.close();
		return plist;
	}
	
	public List<PersonChooseInfoModel> searchPersonChoose(String py)
	{
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		List<PersonChooseInfoModel> plist = new ArrayList<PersonChooseInfoModel>();
		//select displayname ,userguid ,loginid,sex,ou.ouname,title,mobile from user ,ou where  user.ouguid = ou.ouguid and user.userguid = ?"
		Cursor cursor = db.rawQuery(
				"select displayname ,userguid ,loginid,sex,ou.ouname,ou.ouguid,title,mobile from user ,ou where  user.ouguid = ou.ouguid and user.namepyshou like '"+py+"%'",
				null);
		while (cursor.moveToNext()) {
			PersonChooseInfoModel p = new PersonChooseInfoModel();
			p.name = cursor.getString(0);
			p.guid = cursor.getString(1);
			p.type = "人员:"+cursor.getString(4);
			p.parentguid = cursor.getString(5);
			plist.add(p);
		}
		return plist;
	}
	
	public String getUserDisplayNamesByGuids(List<String> list) {
		if(list.size()==0)
		{
			return "";
		}
		String guids = "";
		for(String s:list)
		{
			guids += s+";";
		}
		return getUserDisplayNamesByGuids(guids);
	}
	
	/**
	 * 根据guid，以分号隔开获取人员名称 liyc
	 * 
	 */
	public String getUserDisplayNamesByGuids(String guids) {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = null;
		String names = "";
		String[] guidArr = guids.split(";");
		for (String guid : guidArr) {
			if (!guid.equals("")) {
				cursor = db.rawQuery(
						"select displayname from user where userguid = ?",
						new String[] { guid });
				while (cursor.moveToNext()) {
					names += cursor.getString(0) + ";";
				}
			}
		}
		return names;
	}

	public List<PersonChooseInfoModel> getFilterListByUserguids(Context con,String filterGuids)
	{
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = null;
		String[] userguids = filterGuids.split(";");
		List<PersonChooseInfoModel> list = new ArrayList<PersonChooseInfoModel>();
		for(String userguid:userguids)
		{
			cursor = db.rawQuery(
							"select displayname ,userguid ,loginid,sex,ou.ouname,title,mobile from user ,ou where  user.ouguid = ou.ouguid and user.userguid = ?", new String[]{userguid});
			while(cursor.moveToNext())
			{
				/*HashMap<String, Object> map = new HashMap<String, Object>();
				String title = cursor.getString(0);
				String mobile = cursor.getString(6) == null ? "" : cursor
						.getString(6);
				map.put("title", title);// 人员名称
				map.put("name", cursor.getString(0));
				map.put("mobile", mobile);
				map.put("guid", cursor.getString(1));// 人员编号
				map.put("username", cursor.getString(2));
				map.put("info", "人员");
				String dp = cursor.getString(4);
				if (cursor.getString(5) != null && !cursor.getString(5).equals("")) {
					dp += ":" + cursor.getString(5);
				}
				map.put("dp", dp);
				list.add(map);*/
				PersonChooseInfoModel p = new PersonChooseInfoModel();
				p.name = cursor.getString(0);
				p.guid = cursor.getString(1);
				p.type = "人员:"+cursor.getString(4);
				p.parentguid = "";
				list.add(p);
			}
		}
		return list;
	}
}
